# -*- coding: UTF-8 -*-
"""
    录音
    @Project ：auto_translate_voice
    @File ：auto_record.py
    @Author ：lc
    @Date ：2022.4.21 14:12
"""
import os
import time

from selenium import webdriver
from selenium.webdriver.common.by import By

from config import record_url, record_config_file, start_record_index, record_audio_path, file_prefix
from utils import swipe_web, assert_alert_close, assert_web_open_success, record, get_record_text

webdriver_obj = webdriver.Chrome()
webdriver_obj.maximize_window()
webdriver_obj.get(record_url)
time.sleep(3)
# 网页icon和弹窗
assert_web_open_success(webdriver_obj, (By.XPATH, r"//img[@alt='Microsoft Azure']"),
                        (By.XPATH, "//div[contains(@id,'ModalFocusTrapZone')]"))

time.sleep(2)
swipe_web(webdriver_obj)
time.sleep(2)
assert_alert_close(webdriver_obj, (By.XPATH, "//div[contains(@id,'ModalFocusTrapZone')]"))
time.sleep(2)
# 从头开始重复录音,初始1
file_name_list = list()
for item_data in get_record_text(record_config_file, start_record_index):
    print("录制", item_data[0])
    if item_data[5] != '':
        record(webdriver_obj, item_data[1], item_data[2], item_data[5])
        fileList = os.listdir(record_audio_path)
        print("返回文件列表：", fileList)
        for file_name in fileList:
            if file_prefix not in file_name:
                old_name = record_audio_path + os.sep + file_name
                new_name = record_audio_path + os.sep + item_data[0] + '.mp3'
                os.rename(old_name, new_name)
